<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */



class LostPassword extends HTML_QuickForm {
  private $tpl;
  private $user;

  function LostPassword($tpl,$user) {
    $this->tpl = $tpl;
    $this->user = $user;

    parent::HTML_QuickForm('lostpassword');
    $this->addElement('header', 'header', 'lostpassword');
    $this->addElement('text', 'email', _('email'), array('size' => 30, 'maxlength' => 100));
    $this->addElement('submit', 'btn_Submit', 'Enviar', array('class' => 'btn_submit'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('check_exists', 'function', 'checkExists', $this);
    $this->addRule('email', _('Required field cannot be left blank'),'required',null,1);
		$this->addRule('email', _('verify your email'),'email');
  }

  function checkExists() {
    $this->user->setEmail($this->getSubmitValue('email'));
    return $this->user->checkExists($post='password');
  }

  function displayForm() {
    $this->renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="red" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->user->getMsg());
  }

  function process() {
    $this->user->setEmail($this->getSubmitValue('email'));
    $this->user->setEmailSubject(_('Password recovery'));
    $this->user->setEmailBody(_('If you requested a password recovery for account __email__ on __sitename__ just follow this link:  __url__ otherwise you can ignore this email.'));
    $this->user->lostPassword();
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->user->getMsg());
  }

}

?>